package com.shensi.adminCollect.service.station.impl;

import com.shensi.common.core.domain.entity.SysUser;
import com.shensi.common.core.exception.CustomException;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.utils.StringUtils;
import com.shensi.adminCollect.domain.station.Goods;
import com.shensi.adminCollect.domain.station.form.GoodsForm;
import com.shensi.adminCollect.domain.station.form.GoodsQueryForm;
import com.shensi.adminCollect.domain.station.vo.GoodsQueryVo;
import com.shensi.adminCollect.domain.station.vo.GoodsVo;
import com.shensi.adminCollect.mapper.station.GoodsMapper;
import com.shensi.adminCollect.service.station.GoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public int insertGoods(GoodsForm goodsForm) {
        operaPermission(goodsForm.getEnterpriseId(), goodsForm.getStationId());

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsForm, goods);
        goods.setCreatedBy(SecurityUtils.getUsername());
        goods.setUpdatedBy(SecurityUtils.getUsername());
        return goodsMapper.insertGoods(goods);
    }

    @Override
    public int updateGoods(Long id, GoodsForm goodsForm) {
        operaPermission(goodsForm.getEnterpriseId(), goodsForm.getStationId());

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsForm, goods);
        goods.setId(id);
        goods.setUpdatedBy(SecurityUtils.getUsername());
        goods.setUpdatedTime(new Date());
        return goodsMapper.updateGoods(goods);
    }

    @Override
    public GoodsVo selectGoodsById(Long id) {
        return goodsMapper.selectGoodsById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGoods(List<Long> ids) {
        for (Long id : ids) {
            GoodsVo goodsVo = selectGoodsById(id);
            if (ObjectUtils.isEmpty(goodsVo)) continue;
            operaPermission(goodsVo.getEnterpriseId(), goodsVo.getStationId());

            Goods goods = new Goods();
            goods.setId(id);
            goods.setUpdatedBy(SecurityUtils.getUsername());
            goods.setUpdatedTime(new Date());
            goods.setDel(System.currentTimeMillis());
            goodsMapper.updateGoods(goods);
        }
    }

    @Override
    public List<GoodsQueryVo> pageGoods(GoodsQueryForm queryForm) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userLevel = user.getUserLevel();
        if (StringUtils.isNotEmpty(userLevel) && userLevel.equals("1")) { // 1:油企
            queryForm.setEnterpriseId(user.getEnterpriseId());
        }
        if (StringUtils.isNotEmpty(userLevel) && userLevel.equals("2")) { // 2:油企
            queryForm.setStationId(user.getStationId());
        }
        return goodsMapper.pageGoods(queryForm);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        GoodsVo goodsVo = selectGoodsById(id);
        if (ObjectUtils.isEmpty(goodsVo)) return 0;
        operaPermission(goodsVo.getEnterpriseId(), goodsVo.getStationId());

        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(status);
        goods.setUpdatedBy(SecurityUtils.getUsername());
        goods.setUpdatedTime(new Date());
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 判断当前登录用户是否拥有操作该商品的权限
     * @param enterpriseId 油企id
     * @param stationId 油站id
     */
    private void operaPermission(Long enterpriseId, Long stationId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userLevel = user.getUserLevel();
        if (StringUtils.isNotEmpty(userLevel) && userLevel.equals("1") && ObjectUtils.notEqual(user.getEnterpriseId(), enterpriseId)) // 1:油企
            throw new CustomException("没有权限操作商品！");
        if (StringUtils.isNotEmpty(userLevel) && userLevel.equals("2") && ObjectUtils.notEqual(user.getStationId(), stationId)) // 2:油企
            throw new CustomException("没有权限操作商品！");
    }
}
